﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

using Microsoft.International.Converters.PinYinConverter;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

namespace CommonLibrary.Chinese
{
    public static class ClString
    {
        /// <summary>
        /// 将文本字符串转化为字符数组输出
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        private static char[] GetChars(string in_str)
        {
            char[] Cs = in_str.ToCharArray();
            return Cs;
        }

        /// <summary>
        /// 字符串转拼音
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        public static string ClToPinYins(this string in_str)
        {
            int L = GetChars(in_str).Length;
            if (L == 0) return "";

            string Res = "";
            for (int i = 0; i < L; i++)
            {
                char C = GetChars(in_str)[i];

                if (ChineseChar.IsValidChar(C))
                {
                    ChineseChar CC = new ChineseChar(C);
                    //返回单个汉字拼音的所有集合，包括不同读音
                    ReadOnlyCollection<string> roc = CC.Pinyins;
                    //只获取第一个拼音
                    string Py = CC.Pinyins[0];

                    #region 特殊处理
                    if (C == 24191)
                    {
                        Py = CC.Pinyins[3];//广，取第3个
                    }
                    #endregion


                    Py = Py.Substring(0, 1) + Py.Substring(1).ToLower();

                    //去掉后面的数字，只截取拼音
                    Res += Py.Substring(0, Py.Length - 1) + ",";
                }
                //不是汉字返回问号
                else
                {
                    Res += C + ",";
                }
            }
            //去掉最后一个逗号
            Res = Res.Substring(0, Res.Length - 1);

            return Res;
        }

        /// <summary>
        /// 字符串转拼音首字母
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        public static string ClToPinYinsFirstChar(this string in_str)
        {
            if (in_str.Length == 0)
            {
                return "";
            }

            string[] arr = ClToPinYins(in_str).Split(',');
            string result = "";
            foreach (string s in arr)
            {
                result += s.Substring(0, 1) + ",";
            }
            result = result.Substring(0, result.Length - 1);

            return result;
        }

        /// <summary>
        /// 简体转繁体
        /// </summary>
        /// <param name="in_str"></param>
        /// <returns></returns>
        public static string ClToTraditional(this string in_str)
        {
            return ChineseConverter.Convert(in_str, ChineseConversionDirection.SimplifiedToTraditional);
        }

    }
}
